<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="组织名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入组织名称" :disabled="model.id==''?false:true"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="来源" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="source">
              <j-dict-select-tag type="list" v-model="model.source" dictCode="settlement_org_source" :disabled="model.id==''?false:true" placeholder="请选择来源" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="管理用户" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="users">
              <!-- <j-select-user-by-dep v-model="model.users" :multi="true" /> -->
              <j-search-select-multiple-user v-model="model.users" placeholder="请输入并选择管理用户"></j-search-select-multiple-user>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="包材费(一票多件)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="packageFee">
              <a-input v-model="model.packageFee" placeholder="请输入包材费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="操作费(固定)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operateFee">
              <a-input v-model="model.operateFee" placeholder="请输入操作费"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="技术服务费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="technicalProptions">
              <!-- <a-input v-model="model.technicalProption" placeholder="请输入技术服务费"  ></a-input> -->
              <a-input-number v-model="model.technicalProptions" placeholder="请输入技术服务费"  :min="0" :max="100" :formatter="value => `${value}%`" :parser="value => value.replace('%', '')"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="操作费(基础)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operateBaseFee">
              <a-input v-model="model.operateBaseFee" placeholder="请输入操作费基础"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="操作费(续件)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operateRenewalFee">
              <a-input v-model="model.operateRenewalFee" placeholder="请输入操作费续件"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="推荐人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="recommender">
              <a-input v-model="model.recommender" placeholder="请输入推荐人"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="客户经理" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="customerManager">
              <a-input v-model="model.customerManager" placeholder="请输入客户经理"></a-input>
            </a-form-model-item>
          </a-col>
           <a-col :span="12">
            <a-form-model-item label="邀请码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="affCode">
              <a-input v-model="model.affCode" placeholder="请输入邀请码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="凭证同步金蝶" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="izSyncVoucher">
              <j-dict-select-tag placeholder="请选择凭证同步金蝶" v-model="model.izSyncVoucher" dictCode="yn"/> 
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="启用状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag placeholder="请选择启用状态" v-model="model.status" dictCode="yn"/> 
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="合同" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="businessContract">
               <j-image-upload :isMultiple="false" v-model="model.businessContract" class="idCardImgBack"></j-image-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
import JSearchSelectMultipleUser from '../../../../../components/jeecg/JSearchSelectMultipleUser.vue'

  export default {
    name: 'BaseSettlementOrgForm',
    components: {
        JSearchSelectMultipleUser
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
          id:'',
          technicalProptions:0,
          izSyncVoucher:0,
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入组织名称!'},
              // { validator: (rule, value, callback) => validateDuplicateValue('lg_base_settlement_org', 'name', value, this.model.id, callback)},
           ],
          source: [
              { required: true, message: '请选择结算组织来源!'},
              // { validator: (rule, value, callback) => validateDuplicateValue('lg_base_settlement_org', 'name', value, this.model.id, callback)},
           ],
          // users: [
          //     { required: true, message: '请选择管理用户!'},
              // { validator: (rule, value, callback) => validateDuplicateValue('lg_base_settlement_org', 'name', value, this.model.id, callback)},
           // ],
           technicalProptions: [
               { required: true, message: '请配置技术服务费!'},       
           ],
           izSyncVoucher:[
            { required: true, message: '请选择凭证是否同步到金蝶!'},       
          ],
          status: [
            {required: true, message: '请选择启用状态!'}
            ]
        },
        url: {
          add: "/finance/baseSettlementOrg/add",
          edit: "/finance/baseSettlementOrg/edit",
          queryById: "/finance/baseSettlementOrg/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        // this.edit(this.modelDefault);
        this.model = Object.assign({}, this.modelDefault);
        this.visible = true;
      },
      edit (record) { 
        record.technicalProptions=(record.technicalProption<1)?((record.technicalProption*100).toFixed(2)):record.technicalProption;                                    
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }          
            // this.model.technicalProption=parseFloat(this.model.technicalProptions/100);       
            const baseSettlementOrg={
              id:that.model.id,
              name:that.model.name,
              source:that.model.source,
              users:that.model.users,
              technicalProption:parseFloat(this.model.technicalProptions/100),
              packageFee:that.model.packageFee,
              operateFee:that.model.operateFee,
              operateBaseFee:that.model.operateBaseFee,
              operateRenewalFee:that.model.operateRenewalFee,
              recommender:that.model.recommender,
              customerManager:that.model.customerManager,
              affCode:that.model.affCode,
              businessContract:that.model.businessContract,
              izSyncVoucher: that.model.izSyncVoucher,
              status:that.model.status
            }    
            httpAction(httpurl,baseSettlementOrg,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>